package com.qh.yyxt.pojo;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.qh.yyxt.entity.DelayThing;
import com.qh.yyxt.entity.Flag;
import com.qh.yyxt.entity.GetAndReturn;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.text.SimpleDateFormat;
import java.util.*;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class BorrowThingInfoDTO {

    private DelayThing delayThing;
    //审批详情或者取走详情 或者归还详情 基础上添加了 取走表和归还表
    @JsonProperty("fid")
    private Integer fid;
    @JsonProperty("mes")
    private String mes;
    @JsonProperty("applicant")
    private String applicant;
    @JsonProperty("college")
    private String college;
    @JsonProperty("tel")
    private String tel;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date applyTime;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date startTime;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date endTime;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date passTime;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date returnTime;
    @JsonProperty("approver")
    private String approver;
    //它的state取决去
    @JsonProperty("state")
    private String state;
    private List<TableDTO> list = new ArrayList<>();
    //添加 GetAndReturn
    @JsonProperty("gname")
    private String gname;
    @JsonProperty("gtel")
    private String gtel;
    @JsonProperty("gwatch")
    private String gwatch;
    @JsonProperty("gmes")
    private String gmes;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date gtime;
    @JsonProperty("rname")
    private String rname;
    @JsonProperty("rtel")
    private String rtel;
    @JsonProperty("rwatch")
    private String rwatch;
    @JsonProperty("rmes")
    private String rmes;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date rtime;
    @JsonProperty("examineState")//借用状态
    private String examineState;
    @JsonProperty("activityname")//活动名称
    private String activityname;
    @JsonProperty("damageis")
    private Boolean damageis;//是否损坏
    @JsonProperty("borrowplace")//借用地点
    private String borrowplace;
    @JsonProperty("damage")//损坏情况
    private String damage;
    @JsonProperty("needReturnSigns")
    private String needReturnSigns;//需要归还物品编号
    @JsonProperty("allReturnSigns")
    private String allReturnSigns;//借用的总物品编号
    @JsonProperty("alreadyReturnSign")
    private String alreadyReturnSign;//已经归还的物品编号

    public void setBorrowThingInfoDTO(BorrowThingDTO borrowThingDTO, GetAndReturn getAndReturn) {
        if (borrowThingDTO != null) {
            this.fid = borrowThingDTO.getFid();
            this.mes = borrowThingDTO.getMes();
            this.applicant = borrowThingDTO.getApplicant();
            this.college = borrowThingDTO.getCollege();
            this.tel = borrowThingDTO.getTel();
            this.applyTime = borrowThingDTO.getApplyTime();
            this.startTime = borrowThingDTO.getStartTime();
            this.endTime = borrowThingDTO.getEndTime();
            this.passTime = borrowThingDTO.getPassTime();
            this.approver = borrowThingDTO.getApprover();
            this.state = borrowThingDTO.getState();
            this.returnTime = borrowThingDTO.getReturnTime();
            this.list = borrowThingDTO.getList();
            this.examineState = borrowThingDTO.getExamineState();
            this.activityname = borrowThingDTO.getActivityname();
            this.damageis = borrowThingDTO.getDamageis();
            this.needReturnSigns = borrowThingDTO.getNeedReturnSigns();
            this.allReturnSigns = borrowThingDTO.getAllReturnSigns();
            this.alreadyReturnSign = borrowThingDTO.getAlreadyReturnSign();

        }
        if (getAndReturn != null) {
            this.gname = getAndReturn.getGname();
            this.gtel = getAndReturn.getGtel();
            this.gwatch = getAndReturn.getGwatch();
            this.gmes = getAndReturn.getGmes();
            this.gtime = getAndReturn.getGtime();

            this.rname = getAndReturn.getRname();
            this.rtel = getAndReturn.getRtel();
            this.rwatch = getAndReturn.getRwatch();
            this.rmes = getAndReturn.getRmes();
            this.rtime = getAndReturn.getRtime();
        }
    }

    public void setDelayThing(DelayThing delayThing) {
        this.delayThing = delayThing;
    }

    @JsonIgnore
    public Map<String, String> getMapToWord() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日HH时mm分");
        Map<String, String> map = new HashMap<>();
        map.put("college", college);
        map.put("applicant", applicant);
        map.put("phone", tel);
        Calendar calendar = Calendar.getInstance();
        //设置借用时间
        calendar.setTime(startTime);
        map.put("smonth", (calendar.get(Calendar.MONTH) + 1) + "");
        map.put("sday", (calendar.get(Calendar.DATE)) + "");
        map.put("shour", (calendar.get(Calendar.HOUR_OF_DAY) + ""));
        map.put("sminute", (calendar.get(Calendar.MINUTE)) + "");
        map.put("syear", (calendar.get(Calendar.YEAR)) + "");
        //设置结束时间
        calendar.setTime(endTime);
        map.put("emonth", (calendar.get(Calendar.MONTH) + 1) + "");
        map.put("eday", (calendar.get(Calendar.DATE)) + "");
        map.put("ehour", (calendar.get(Calendar.HOUR_OF_DAY) + ""));
        map.put("eminute", (calendar.get(Calendar.MINUTE)) + "");
        map.put("eyear", (calendar.get(Calendar.YEAR)) + "");
        //借用原因
        map.put("reason", mes);
        //借用的物品
        String thing = "";
        //判断是否损坏
        int badNum = 0;
        for (TableDTO tableDTO : list) {
            if (tableDTO.getBadNum() > 0)
                badNum += tableDTO.getBadNum();
            thing += tableDTO.getInfo() + ";";
        }
        if (thing.length() > 0)
            thing = thing.substring(0, thing.length() - 1);
        if (badNum > 0) {
            map.put("bad2", "是");
        } else {
            map.put("bad2", "否");
        }
        map.put("thing", thing);
        String bad1 = "";
        //如果是待审批的
        if (state.equals("待审批")) {
            approver = "";
            gname = "";
            gtel = "";
            gwatch = "";
            gmes = "";
            map.put("bad2", "");
            rname = "";
            rtel = "";
            rwatch = "";
            rmes = "";
        } else if (state.equals("审批通过") || state.equals("未通过")) {
            gname = "";
            gtel = "";
            gwatch = "";
            gmes = "";
            map.put("bad2", "");
            rname = "";
            rtel = "";
            rwatch = "";
            rmes = "";
        } else if (state.equals("未归还")) {
            bad1 = "否";
            map.put("bad2", "");
            rname = "";
            rtel = "";
            rwatch = "";
            rmes = "";
        } else {
            bad1 = "否";
        }
        map.put("bad1", bad1);
        //审批人
        map.put("approver", approver);
        //设置审批时间
        if (passTime != null)
            map.put("passTime", format.format(passTime));
        else
            map.put("passTime", "");

        //取走部分的信息
        map.put("gname", gname);
        map.put("gtel", gtel);
        map.put("gwatch", gwatch);
        if (gtime != null)
            map.put("gtime", format.format(gtime));
        else
            map.put("gtime", "");
        //归还部分的信息
        map.put("rname", rname);
        map.put("rtel", rtel);
        map.put("rwatch", rwatch);
        if (rtime != null)
            map.put("rtime", format.format(rtime));
        else
            map.put("rtime", "");
        //设置备注
        map.put("gmes", gmes);
        map.put("rmes", rmes);
        return map;
    }
}
